//
//  LMCommentController.m
//  舍品
//
//  Created by 楼 眉 on 16/2/26.
//  Copyright © 2016年 楼 眉. All rights reserved.
//
#pragma mark - 添加评论控制器
static int bottomToolViewHeight = 49;

#import "LMCommentController.h"
#import "LMPost.h"
#import "LMReply.h"
#import "LMAlertLabel.h"
#import "LMReply.h"
#import <SVProgressHUD.h>

@interface LMCommentController ()

@property (nonatomic,weak)  UIImageView *bottomToolView;
@property (nonatomic,strong)  LMPost *post;
@property (nonatomic,strong)  LMReply *reply;
@property (nonatomic,strong)  UITextView *textView;

@end

@implementation LMCommentController

+ (instancetype)LMCommentControllerWIthPost:(LMPost *)post
{
    LMCommentController *lmCc = [[LMCommentController alloc]init];
    lmCc.post = post;
    return lmCc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupPostSubViews];
    [self listenToKeyBoard];
}


#pragma mark - 创建post子控件
- (void)setupPostSubViews
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *topView = [[UIView alloc]init];
    topView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 20 + 44);
    topView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:topView];
    
    UILabel *topLabel = [[UILabel alloc]init];
    topLabel.text = @"添加评论";
    topLabel.textAlignment = NSTextAlignmentCenter;
    topLabel.textColor = [UIColor whiteColor];
    topLabel.frame = CGRectMake(0, 0, 100, 33);
    topLabel.center = topView.center;
    topLabel.y = topView.height - topLabel.height - 5;
    [topView addSubview: topLabel];
    
    UIButton *backBtn = [[UIButton alloc]init];
    backBtn.frame = CGRectMake(0, topLabel.y, 50, 33);
    [backBtn setTitle:@"取消" forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:backBtn];
    
    UIButton *issueBtn = [[UIButton alloc]init];
    issueBtn.frame = CGRectMake(topView.width - 50, topLabel.y, 50, 33);
    [issueBtn setTitle:@"发表" forState:UIControlStateNormal];
    [issueBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [issueBtn addTarget:self action:@selector(published) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:issueBtn];
    
    UITextView *textView = [[UITextView alloc]init];
    textView.frame = CGRectMake(5, 20 + 44 +10, SCREEN_WIDTH - 10, 200);
    textView.font = [UIFont systemFontOfSize:15];
    [textView becomeFirstResponder];
    self.textView = textView;
    [self.view addSubview:textView];
    
    
    //底部工具栏
//    UIImageView *bottomToolView = [[UIImageView alloc]init];
//    bottomToolView.frame =CGRectMake(0, SCREEN_HEIGHT - bottomToolViewHeight, SCREEN_WIDTH, bottomToolViewHeight);
//    bottomToolView.userInteractionEnabled = YES;
//    bottomToolView.backgroundColor = [UIColor lightGrayColor];
//    self.bottomToolView = bottomToolView;
//    [self.view addSubview:bottomToolView];
    
//    UIButton *btn = [[UIButton alloc]init];
//    btn.frame = CGRectMake(0, 0, bottomToolView.width / 2, bottomToolView.height);
//    [btn setTitle:@"插入图片" forState:UIControlStateNormal];
//    [btn addTarget:self action:@selector(addPicture) forControlEvents:UIControlEventTouchUpInside];    [bottomToolView addSubview:btn];
}

#pragma mark - 监听键盘
- (void)listenToKeyBoard
{
    //监听键盘点击
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
}

#pragma mark - 发布评论
- (void)published
{
    [self.view endEditing:YES];
        if (self.textView.text == NULL) {
            [LMAlertLabel alertLabelWithTarget:self title:@"请输入内容"];
            return;
        }
        [SVProgressHUD showWithStatus:@"加载中"];
        AVRelation *relation = [self.post relationForKey:@"reviews"];
        LMUser *user = [LMUser currentUser];
        LMReply *reply = [LMReply object];
        reply.user = user;
        reply.replyText = self.textView.text;
    __weak typeof(self) weakSelf = self;
        [reply saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            [relation addObject:reply];
            [weakSelf.post saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    [SVProgressHUD dismiss];
                    [LMAlertLabel alertLabelWithTarget:self title:@"评论成功"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [weakSelf dismissViewControllerAnimated:YES completion:^{
                            [weakSelf commentComplete];
                        }];
                    });
                }
                else
                {
                    [SVProgressHUD dismiss];
                    [LMAlertLabel alertLabelWithTarget:self title:@"评论失败"];
                }
            }];
        }];

}
- (NSString *)getTextViewText
{
    return self.textView.text;
}
#pragma mark - 返回上一页
- (void)backBtnClick
{
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma mark - 方便ARC使用代理，无其他作用，故为空
- (void)commentComplete
{}
#pragma mark - 添加图片
- (void)addPicture
{
    NSLog(@"插入图片");
}
#pragma mark - 根据键盘位置变化计算控件位置
- (void)keyboardWillChangeFrameNotification:(NSNotification *)note
{
    CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    [UIView animateWithDuration:0 animations:^{
        self.bottomToolView.y = CGRectGetMinY(keyboardFrame) - bottomToolViewHeight;
    }];
}
#pragma marks - Warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - 释放监听
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"dealoc--commentVc");
}
@end
